﻿using System;
using DL.Animation;
using DL.Characters.Player.FSM.StateMachine;
using DL.Tools;
using UnityEngine;
using UnityEngine.InputSystem;

namespace DL.Characters.Player.FSM.State
{
    [Obsolete("")]
    public class PlayerIdleState : PlayerOnGroundState
    {
        public PlayerIdleState(PlayerMoveStateMachine playerStateMachine, int property = 0) : base(playerStateMachine,
            property)
        {
        }

        public override void Enter()
        {
            base.Enter();
            Animator.SetBool(AnimatorParameter.HasInput, false);
            ReusableData.InputMult = 0;
            Animator.CrossFadeInFixedTime("MotionState", 0.25f);
        }

        public override bool StateTransitionCheck()
        {
            if (ReusableData.InputDirection != Vector2.zero)
            {
                StateMachine.ChangeState<PlayerLocomotionState>();
                return true;
            }

            return false;
        }

        public override void HandleInput()
        {
            base.HandleInput();
            Animator.SetFloat(AnimatorParameter.Movement, MoveController.CurrentMovementVelocity.Value);
            Animator.SetFloat(AnimatorParameter.Horizontal, ReusableData.InputDirection.x, 0.2f, Time.deltaTime);
            Animator.SetFloat(AnimatorParameter.Vertical, ReusableData.InputDirection.y, 0.2f, Time.deltaTime);
        }

        protected override void AddInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.started += EnterLocomotionStart;
            StateMachine.InputSystem._playerInputActions.LeftShift.started += OnEnterDodgeState;
        }


        protected override void RemoveInputActionCallBacks()
        {
            StateMachine.InputSystem._playerInputActions.Movement.started -= EnterLocomotionStart;
            StateMachine.InputSystem._playerInputActions.LeftShift.started -= OnEnterDodgeState;
            NewTimerManager.Inst.CalcelTimer(m_GameTimer);
        }

        private GameTimer m_GameTimer;

        private void EnterLocomotionStart(InputAction.CallbackContext context)
        {
            m_GameTimer = NewTimerManager.Inst.GetTimer(0.11f, CheckMoveInput);
        }

        private void CheckMoveInput()
        {
            if (StateMachine.InputSystem.Movement == Vector2.zero)
            {
                Animator.CrossFadeInFixedTime("Run_F_Stop", ReusableData.AniTransitionTime);
            }
            else
            {
                StateMachine.ChangeState<PlayerLocomotionState>();
            }
        }
        
        protected void OnEnterDodgeState(InputAction.CallbackContext context)
        {
            StateMachine.ChangeState<PlayerDodgeState>();
        }
    }
}